package com.qf.mvc01.ex;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice //代表当前的统一异常处理器要返回的是数据而不是页面
public class MyRestExHandler {

    @ExceptionHandler(UserException.class)
    public Map<String,String> userExHandler(UserException e){
        System.out.println(e);
        HashMap<String,String> map = new HashMap<>();

        map.put("code",e.getCode());
        map.put("msg",e.getMsg());
        map.put("date",new Date().toString());

        return map;

    }

    @ExceptionHandler(Exception.class)
    public Map<String,String> exExHandler(Exception e){
        System.out.println(e);
        HashMap<String,String> map = new HashMap<>();

        map.put("code","-1000");
        map.put("msg","加钱处理");
        map.put("date",new Date().toString());

        return map;

    }
}
